<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名字" prop="sickName">
        <el-input
          v-model="queryParams.sickName"
          placeholder="请输入名字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="就诊号" prop="visitNo">
        <el-input
          v-model="queryParams.visitNo"
          placeholder="请输入就诊号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="手麻对应的id" prop="aisSickId">
        <el-input
          v-model="queryParams.aisSickId"
          placeholder="请输入手麻对应的id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:sickperson:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:sickperson:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:sickperson:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:sickperson:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sickpersonList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="病人编码" align="center" prop="sourceId" />
      <el-table-column label="姓名" align="center" prop="sickName" />
      <el-table-column label="性别" align="center" prop="sickSex" />
      <el-table-column label="年龄" align="center" prop="age">
        <template #default="scope">
          <span>{{ scope.row.age }}</span>
          <span>{{ scope.row.ageUnit }}</span>
        </template>
      </el-table-column>
      <el-table-column label="体重" align="center" prop="weight" />
      <el-table-column label="身高" align="center" prop="height" />
      <el-table-column label="出生时间" align="center" prop="birthday" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="床号" align="center" prop="bedNew" />
      <el-table-column label="电话" align="center" prop="telephone" />
      <el-table-column label="收费类型" align="center" prop="chargeDesc" />
      <el-table-column label="军人标识" align="center" prop="priorityName" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="就诊号" align="center" prop="visitNo" />
      <el-table-column label="代办人" align="center" prop="agentName" />
      <el-table-column label="科室" align="center" prop="depCode" />
      <el-table-column label="国籍" align="center" prop="countryDesc" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['manage:sickperson:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['manage:sickperson:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="sickpersonRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编码" prop="sourceId">
          <el-input v-model="form.sourceId" placeholder="请输入编码" />
        </el-form-item>
        <el-form-item label="名字" prop="sickName">
          <el-input v-model="form.sickName" placeholder="请输入名字" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="单位" prop="ageUnit">
          <el-input v-model="form.ageUnit" placeholder="请输入年龄单位" />
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="身高" prop="height">
          <el-input v-model="form.height" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="血型rh" prop="bloodRh">
          <el-input v-model="form.bloodRh" placeholder="请输入血型rh" />
        </el-form-item>
        <el-form-item label="名族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入名族" />
        </el-form-item>
        <el-form-item label="出生时间" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择出生时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="上次床号" prop="bedOld">
          <el-input v-model="form.bedOld" placeholder="请输入上次床号" />
        </el-form-item>
        <el-form-item label="最新床号" prop="bedNew">
          <el-input v-model="form.bedNew" placeholder="请输入最新床号" />
        </el-form-item>
        <el-form-item label="住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入住址" />
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="收费类型" prop="chargeCode">
           <el-select v-model="form.chargeCode" placeholder="请选择">
            <el-option
              v-for="dict in manage_charge_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="军人标识" prop="priorityName">
          <el-input v-model="form.priorityName" placeholder="请输入军人标识" />
        </el-form-item>
        <el-form-item label="就诊号" prop="visitNo">
          <el-input v-model="form.visitNo" placeholder="请输入就诊号" />
        </el-form-item>
        <el-form-item label="手麻id" prop="aisSickId">
          <el-input v-model="form.aisSickId" placeholder="请输入手麻对应的id" />
        </el-form-item>
        <el-form-item label="代办人身份证" prop="agentIdcar">
          <el-input v-model="form.agentIdcar" placeholder="请输入代办人身份证" />
        </el-form-item>
        <el-form-item label="代办人姓名" prop="agentName">
          <el-input v-model="form.agentName" placeholder="请输入代办人姓名" />
        </el-form-item>
        <el-form-item label="婚姻状态" prop="marryDesc">
          <el-input v-model="form.marryDesc" placeholder="请输入婚姻状态" />
        </el-form-item>
        <el-form-item label="所在科室" prop="depCode">
          <el-input v-model="form.depCode" placeholder="请输入所在科室" />
        </el-form-item>
        <el-form-item label="国籍" prop="countryDesc">
          <el-input v-model="form.countryDesc" placeholder="请输入国籍" />
        </el-form-item>
        <el-form-item label="健康号" prop="healthCard">
          <el-input v-model="form.healthCard" placeholder="请输入健康号" />
        </el-form-item>
        <el-form-item label="病案号" prop="documentNo">
          <el-input v-model="form.documentNo" placeholder="请输入病案号" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Sickperson">
import { listSickperson, getSickperson, delSickperson, addSickperson, updateSickperson } from "@/api/manage/sickperson";

const { proxy } = getCurrentInstance();
const { manage_charge_type } = proxy.useDict('manage_charge_type');
const sickpersonList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sickName: null,
    idCard: null,
    visitNo: null,
    aisSickId: null,
  },
  rules: {
    sickPersonId: [
      { required: true, message: "自增ID不能为空", trigger: "blur" }
    ],
    sourceId: [
      { required: true, message: "his病人id不能为空", trigger: "blur" }
    ],
    sickName: [
      { required: true, message: "名字不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询列表 */
function getList() {
  loading.value = true;
  listSickperson(queryParams.value).then(response => {
    sickpersonList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    sickPersonId: null,
    sourceId: null,
    visitId: null,
    sickName: null,
    sickSex: null,
    age: null,
    ageUnit: null,
    weight: null,
    height: null,
    bloodType: null,
    bloodRh: null,
    nation: null,
    birthday: null,
    idCard: null,
    bedOld: null,
    bedNew: null,
    address: null,
    telephone: null,
    chargeCode: null,
    chargeDesc: null,
    priorityName: null,
    status: null,
    visitNo: null,
    aisSickId: null,
    delFlag: null,
    agentIdcar: null,
    agentName: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    marryDesc: null,
    depCode: null,
    countryDesc: "中国",
    healthCard: null,
    documentNo: null,
    deadTime: null
  };
  proxy.resetForm("sickpersonRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.sickPersonId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _sickPersonId = row.sickPersonId || ids.value
  getSickperson(_sickPersonId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["sickpersonRef"].validate(valid => {
    if (valid) {
      if (form.value.sickPersonId != null) {
        updateSickperson(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSickperson(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _sickPersonIds = row.sickPersonId || ids.value;
  proxy.$modal.confirm('是否确认删除编号为"' + _sickPersonIds + '"的数据项？').then(function() {
    return delSickperson(_sickPersonIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/sickperson/export', {
    ...queryParams.value
  }, `sickperson_${new Date().getTime()}.xlsx`)
}

getList();
</script>
